package com.infore.dataReceiving.netty;
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.infore.dataReceiving.core.InstructionsHandle;
import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;

import io.netty.channel.ChannelHandlerContext;  
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.channel.ChannelHandler.Sharable;

@Component("serverHandler")
@Sharable
public class ServerHandler extends SimpleChannelInboundHandler<String> {  
  
    private static final Logger log = LoggerFactory.getLogger(ServerHandler.class);  
    
    @Autowired 
    //@Qualifier("air212InstructionsHandle")
    @Qualifier("water2018InstructionsHandle")
	private InstructionsHandle instructionsHandle;
   
      
    @Override  
    public void channelRead0(ChannelHandlerContext ctx, String instruction)  
            throws Exception {  
    	instruction = instruction+"\r\n";
    	instructionsHandle.handle(instruction,ctx);
    	System.out.println(instruction);
    	
    	
    	//1,消息验证
    	//2，消息解析
    	
//    	 log.info("client msg:"+msg+"线程："+Thread.currentThread().getName());
//         String clientIdToLong= ctx.channel().id().asLongText();
//         log.info("client long id:"+clientIdToLong);
//         String clientIdToShort= ctx.channel().id().asShortText();
//         log.info("client short id:"+clientIdToShort);
//         if(msg.indexOf("bye")!=-1){
//             //close
//             ctx.channel().close();
//         }else{
//             //send to client
//             ctx.channel().writeAndFlush("Your msg is:"+msg);
//
//         }
    }  
    @Override  
    public void channelActive(ChannelHandlerContext ctx) throws Exception {  
          
//    	log.info("RamoteAddress : " + ctx.channel().remoteAddress() + " active !"+Thread.currentThread().getName());
//
//        ctx.channel().writeAndFlush( "Welcome to " + InetAddress.getLocalHost().getHostName() + " service!\n");

        super.channelActive(ctx);
//        ctx.channel().close();
//        Thread.currentThread().stop();
    }  
      
  
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    	String mn = Air212InstructionsHandleUtil.channelMap.get(ctx.channel().id().asLongText());
    	if(mn!=null) {
    		Air212InstructionsHandleUtil.cMap.remove("air_"+mn);
    	}
        cause.printStackTrace();  
        ctx.close();  
    }  
  
    @Override  
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    	String mn = Air212InstructionsHandleUtil.channelMap.get(ctx.channel().id().asLongText());
    	if(mn!=null) {
    		Air212InstructionsHandleUtil.cMap.remove("air_"+mn);
    	}
        log.info("\nChannel is disconnected");  
        ctx.channel().close();
    }  
    
    
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
            if (evt instanceof IdleStateEvent) {
                IdleStateEvent event = (IdleStateEvent) evt;
             if (event.state().equals(IdleState.READER_IDLE))
                     ctx.close();
            //标志该链接已经close 了 
            }
        }
}
